package com.kfm.util;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;

@Data
public class Resp extends HashMap<String,Object> {

    public Resp() {
    }

    public Resp(int code, String msg, Object data) {
        super.put("code", code);
        super.put("message", msg);
        super.put("data", data);
    }


    public static Resp ok(){
        Resp resp = new Resp(200, "ok", null);
        return resp;
    }

    public static Resp ok(Object data){
        return new Resp(200, "ok", data);
    }

    public static Resp error(String msg){
        return new Resp(500, msg, null);
    }

    public static Resp instance(int code, String msg, Object data){
        return new Resp(code, msg, data);
    }

    @Override
    public Resp put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static void main(String[] args) {
        Resp resp = Resp.ok("123").put("name", "kfm").put("age",18);
        ObjectMapper objectMapper = new ObjectMapper();

        try {
            System.out.println(objectMapper.writeValueAsString(resp));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}
